package com.ctgu.tengyun.service.faultsolution.impl;

import com.ctgu.tengyun.dao.mybatis.FaultSolutionMapper;
import com.ctgu.tengyun.domain.FaultSolution;
import com.ctgu.tengyun.domain.FaultSolutionExample;
import com.ctgu.tengyun.service.faultsolution.FaultSolutionService;
import com.ctgu.tengyun.util.CollectionsUtil;
import com.ctgu.tengyun.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by liaoweizhi on 2019/8/17.
 */
@Service
public class FaultSolutionServiceImpl implements FaultSolutionService {

    @Resource
    private FaultSolutionMapper mapper;

    @Override
    public int insertSelective(FaultSolution record) {
        return mapper.insertSelective(record);
    }

    @Override
    public int upsert(FaultSolution record) {
        return mapper.upsert(record);
    }

    @Override
    public FaultSolution selectByRefIdAndRefType(String refId, Integer refType) {
        FaultSolutionExample example = new FaultSolutionExample();
        FaultSolutionExample.Criteria criteria = example.createCriteria();

        criteria.andRefIdEqualTo(refId);
        criteria.andRefTypeEqualTo(refType);

        List<FaultSolution> faultSolutions = mapper.selectByExample(example);
        if (!CollectionsUtil.isNullOrEmpty(faultSolutions)) {
            return faultSolutions.get(0);
        }

        return null;
    }

    @Override
    public Integer countFaultSolution(String refId, Integer refType, String keyWords) {
        FaultSolutionExample example = new FaultSolutionExample();
        FaultSolutionExample.Criteria criteria1 = example.createCriteria();
        FaultSolutionExample.Criteria criteria2 = example.createCriteria();
        FaultSolutionExample.Criteria criteria3 = example.createCriteria();
        FaultSolutionExample.Criteria criteria4 = example.createCriteria();

        if(StringUtils.isNotEmpty(refId)){
            criteria1.andRefIdEqualTo(refId);
        }
        if(null != refType){
            criteria1.andRefTypeEqualTo(refType);
        }

        if(StringUtils.isNotEmpty(keyWords)){
            if(StringUtils.isNotEmpty(refId)){
                criteria2.andRefIdEqualTo(refId);
                criteria3.andRefIdEqualTo(refId);
                criteria4.andRefIdEqualTo(refId);
            }

            if(null != refType){
                criteria2.andRefTypeEqualTo(refType);
                criteria3.andRefTypeEqualTo(refType);
                criteria4.andRefTypeEqualTo(refType);
            }

            criteria1.andTroubleTypeLike("%" + keyWords +"%");
            criteria2.andTroubleInfoLike("%" + keyWords +"%");
            criteria3.andSolutionLike("%" + keyWords +"%");
            criteria4.andTroubleReasonLike("%" + keyWords +"%");
            example.or(criteria2);
            example.or(criteria3);
            example.or(criteria4);
        }

        return mapper.countByExample(example);
    }

    @Override
    public List<FaultSolution> selectFaultSolutionList(String refId, Integer refType, String keyWords, Integer pageNo, Integer pageSize) {
        FaultSolutionExample example = new FaultSolutionExample();
        FaultSolutionExample.Criteria criteria1 = example.createCriteria();
        FaultSolutionExample.Criteria criteria2 = example.createCriteria();
        FaultSolutionExample.Criteria criteria3 = example.createCriteria();
        FaultSolutionExample.Criteria criteria4 = example.createCriteria();

        if(StringUtils.isNotEmpty(refId)){
            criteria1.andRefIdEqualTo(refId);
        }
        if(null != refType){
            criteria1.andRefTypeEqualTo(refType);
        }

        if(StringUtils.isNotEmpty(keyWords)){
            if(StringUtils.isNotEmpty(refId)){
                criteria2.andRefIdEqualTo(refId);
                criteria3.andRefIdEqualTo(refId);
                criteria4.andRefIdEqualTo(refId);
            }

            if(null != refType){
                criteria2.andRefTypeEqualTo(refType);
                criteria3.andRefTypeEqualTo(refType);
                criteria4.andRefTypeEqualTo(refType);
            }

            criteria1.andTroubleTypeLike("%" + keyWords +"%");
            criteria2.andTroubleInfoLike("%" + keyWords +"%");
            criteria3.andSolutionLike("%" + keyWords +"%");
            criteria4.andTroubleReasonLike("%" + keyWords +"%");
            example.or(criteria2);
            example.or(criteria3);
            example.or(criteria4);
        }

        example.setStartIndex((pageNo - 1) * pageSize);
        example.setPageSize(pageSize);

        return mapper.selectByExample(example);
    }

    @Override
    public FaultSolution selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(FaultSolution record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

}
